package com.baihan.angus.app.game.common.util.media;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.baihan.angus.app.game.basic.widgets.TitleView;
import com.baihan.angus.app.game.common.R;
import com.gyf.immersionbar.ImmersionBar;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MediaGalleryActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TitleView titleView;
    private List<IMediaInfo> mMediaInfos;
    private ArrayList<MediaFragment> fragments;
    private int initPosition;
    private int type;//下载按钮是否显示 0-显示 1-不显示
    private ImageButton btnSave;

    public static void start(Context context, List<IMediaInfo> mediaInfos, int position, int type) {
        Intent intent = new Intent(context, MediaGalleryActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("media", (Serializable) mediaInfos);
        bundle.putInt("position", position);
        bundle.putInt("type", type);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_media);

        ImmersionBar.with(this).fitsSystemWindows(true).statusBarDarkFont(true).statusBarColor("#FFFFFF").init();

        mMediaInfos = (List<IMediaInfo>) getIntent().getSerializableExtra("media");
        initPosition = getIntent().getIntExtra("position", 0);
        type = getIntent().getIntExtra("type", 0);
        if (mMediaInfos == null || mMediaInfos.size() == 0) {
            finish();
            return;
        }
        btnSave = findViewById(R.id.btnSave);
        titleView = findViewById(R.id.titleView);
        viewPager = findViewById(R.id.viewPager);

        //下载按钮是否显示
        if (type == 0) {
            btnSave.setVisibility(View.VISIBLE);
        } else if (type == 1) {
            btnSave.setVisibility(View.GONE);
        }

        fragments = new ArrayList<>();
        for (IMediaInfo iMediaInfo : mMediaInfos) {
            fragments.add(MediaFragment.newInstance(iMediaInfo));
        }
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return mMediaInfos.size();
            }
        });
        viewPager.setOffscreenPageLimit(4);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                titleView.setTitleText((position + 1) + "/" + mMediaInfos.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(initPosition);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragments.get(viewPager.getCurrentItem()).saveFile();
            }
        });

        titleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}